﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using GoogleEarthClasses.Global;
using GoogleEarthClasses.Data;
using GoogleEarthClasses.Entities;
namespace GoogleEarthClasses.Logic
{
    public class LPolygonType
    {
        /// <summary>
        /// connect to the server and download the polygons available
        /// </summary>
        /// <returns></returns>
        public static List<EPolygonType> SelectAll()
        {
            List<EPolygonType> m_PolygonTypes = new List<EPolygonType>();
            EPolygonType m_PolygonType = new EPolygonType();
            string m_PrevPolygonTypeName = "";

            DataTable _Datatable = DPolygonType.SelectAll();
            foreach (DataRow dr in _Datatable.Select())
            {
                if (m_PrevPolygonTypeName != ConvertHelper.Tostring(dr["TypeName"]))
                {
                    m_PrevPolygonTypeName = ConvertHelper.Tostring(dr["TypeName"]);
                    m_PolygonType = new EPolygonType();

                    m_PolygonType.TypeName = ConvertHelper.Tostring(dr["TypeName"]);
                    m_PolygonType.FillOpacity = ConvertHelper.Toint(dr["FillOpacity"]);
                    m_PolygonType.FillRgb = ConvertHelper.Tostring(dr["FillRgb"]);
                    m_PolygonType.LineRgb = ConvertHelper.Tostring(dr["LineRgb"]);
                    m_PolygonType.LineWidth = ConvertHelper.Toint(dr["LineWidth"]);
                    m_PolygonType.Polygons = SelectAllPolygons(_Datatable, m_PrevPolygonTypeName);
                    m_PolygonTypes.Add(m_PolygonType);
                }
            }
            return m_PolygonTypes;
        }
        private static List<EPolygon> SelectAllPolygons(DataTable _Datatable, string _PolygonName)
        {
            List<EPolygon> m_Polygons = new List<EPolygon>();
            EPolygon m_Polygon;
            foreach (DataRow dr in _Datatable.Select())
            {
                if (ConvertHelper.Tostring(dr["TypeName"]) == _PolygonName && 
                        ConvertHelper.Toint(dr["PolygonId"]) != Int32.MinValue)
                {
                    m_Polygon = new EPolygon();
                    m_Polygon.CreatedAt = ConvertHelper.Tostring(dr["CreatedAt"]);
                    m_Polygon.CreatedBy = ConvertHelper.Tostring(dr["CreatedBy"]);
                    m_Polygon.CreatedOn = ConvertHelper.ToDateTime(dr["CreatedOn"]);
                    m_Polygon.Description = ConvertHelper.Tostring(dr["Description"]);
                    m_Polygon.Name = ConvertHelper.Tostring(dr["PolygonName"]);
                    m_Polygon.Points = ConvertHelper.Tostring(dr["Points"]);
                    m_Polygon.PolygonId = ConvertHelper.Toint(dr["PolygonId"]);
                    m_Polygon.TypeName = ConvertHelper.Tostring(dr["TypeName"]);
                    m_Polygon.UpdatedAt = ConvertHelper.Tostring(dr["UpdatedAt"]);
                    m_Polygon.UpdatedBy = ConvertHelper.Tostring(dr["UpdatedBy"]);
                    m_Polygon.UpdatedOn = ConvertHelper.ToDateTime(dr["UpdatedOn"]);
                    m_Polygons.Add(m_Polygon);
                }
            }
            return m_Polygons;
        }
    }
}
